﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.Utilities;
using System.Data;
using System.Globalization;


namespace zevenseas.ThemeConfig
{
    public static class Extensions
    {
        public static DataView GetAllThemes(this SPWebApplication typeToTarget)
        {
            DataView allDefaultThemes = GetDefault();
            DataView allCustomThemes = GetCustom(typeToTarget);

            DataTable merged = new DataTable();
            merged = allDefaultThemes.Table.Clone();

            merged.Merge(allDefaultThemes.Table);
            merged.Merge(allCustomThemes.Table);

            return new DataView(merged);
        }

        public static DataView GetDefaultThemes(this SPWebApplication typeToTarget)
        {
            return GetDefault();
        }

        public static DataView GetCustomThemes(this SPWebApplication typeToTarget)
        {
            return GetCustom(typeToTarget);
        }

        public static DataView GetDefault()
        {
            string filePath = String.Format("{0}\\LAYOUTS\\1033\\SPThemes.xml", SPUtility.GetGenericSetupPath("TEMPLATE"));

            SPThemes spThemes = new SPThemes();
            spThemes.DataSetName = "SPThemes";
            spThemes.Locale = new CultureInfo("en-US");
            spThemes.Namespace = "http://tempuri.org/SPThemes.xsd";
            spThemes.ReadXml(filePath);

            DataView view = new DataView(spThemes.Tables[0]);
            view.Sort = "DisplayName";

            return view;
        }

        public static DataView GetCustom(SPWebApplication webApplication)
        {
            List<string> filePaths = new List<string>();
            filePaths = GetOtherXmlDefinitions(webApplication);

            DataTable dataTable = new DataTable();
            dataTable = ProcessOtherXmlDefinitions(filePaths);

            return new DataView(dataTable);
        }

        private static List<string> GetOtherXmlDefinitions(SPWebApplication webApplication)
        {
            List<string> filePaths = new List<string>();
                        
            ThemesXmlFileLocations themeFileLocations = webApplication.GetChild<ThemesXmlFileLocations>("ThemesXmlFileLocations");
            if (themeFileLocations != null)
            {                
                filePaths = themeFileLocations.FilePaths;
            }

            return filePaths;
        }

        private static DataTable ProcessOtherXmlDefinitions(List<string> filePaths)
        {
            DataTable dataTable = new DataTable();
            
            foreach (string filePath in filePaths)
            {
                if (!string.IsNullOrEmpty(filePath))
                {
                    string customFilePath = SPUtility.GetGenericSetupPath(filePath);
                    
                    SPThemes spThemes = new SPThemes();
                    spThemes.DataSetName = "SPThemes";
                    spThemes.Locale = new CultureInfo("en-US");
                    spThemes.Namespace = "http://tempuri.org/SPThemes.xsd";
                    spThemes.ReadXml(customFilePath);

                    dataTable = spThemes.Tables[0].Clone();

                    foreach (DataRow row in spThemes.Tables[0].Rows)
                    {
                        DataRow customThemeRow = dataTable.NewRow();
                        customThemeRow.ItemArray = row.ItemArray;
                        dataTable.Rows.Add(customThemeRow);
                    }
                }
            }
            

            return dataTable;
        }        
    }       
}
